<?
include_once("/home/gru/public_html_dev/lib/common.php");

$connect = mysql_connect("localhost","root","qhdksdusrntlf", true) or die( "error DB connection");
mysql_select_db('gru', $connect);
mysql_query("set names utf8");


function cropImg($imgURL,$w,$h,$f_er,$name){
	$today = date("Ymd");
	$dir = '/home/gru/img/'.$f_er.'/'.$today.'/';
	$file= '/home/gru/img/'.$f_er.'/'.$name;
		
	if(!file_exists($dir)){
		if(!mkdir($dir)){
			echo "error mkdir get.php";
			return 0;
		}
	}
  
  	if($imgURL=="")return;	
	if(is_file($file))return;
	
	debug_flush("->>".$file);
	
	try{		
	  $im = new Imagick();
	  $im->readImage($imgURL);
	  $im->cropThumbnailImage ( $w, $h );
	  
	  $im->writeImage($file);
	  $im->destroy();
	}catch(Exception $e){
		echo $e->getMessage()."<br>";
	}
}


//-----------------------------------------------------------------------------
// 섬네일 업데이트
//-----------------------------------------------------------------------------
$opt = "";
//$opt = " where id=7835";
$Query  = " SELECT id,mimg,mImgS,pid,domain FROM coupon $opt";
$Result = mysql_query($Query);
$RecCnt = mysql_num_rows($Result);


while ($Record = mysql_fetch_array($Result))
{
	$id		= $Record['id'];
	$mimg	= $Record['mimg'];
	$mImgS	= $Record['mImgS'];
	$pid	= $Record['pid'];
	$domain	= $Record['domain'];
	
	
	$today = date("Ymd");
	$file = str_replace("/","_",$domain."_".$pid).".jpg";
	$mImgS = $today.'/'.$file;//섬네일경로
	
	//섬네일 생성
	cropImg($mimg,300,160,"s",$mImgS);
	
	$sQuery  = " UPDATE coupon SET mImgS = '$mImgS' WHERE id = $id";
	debug_flush($sQuery);
	$sResult = mysql_query($sQuery);
}


mysql_close($connect);
?>